﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Threading;
using System.Web.UI.WebControls;
using Medianamik.Core;
using Medianamik.Core.Sugar;
using Medianamik.UI.Controls;
using Medianamik.UI.Web.MVP.View;
using Medianamik.UI.Web.Shop.Data;
using Telerik.Web.UI;

namespace Medianamik.UI.Web.Shop
{
    public partial class ShopProducts : ModuleViewWithGrid<IProducts>, IProducts
    {
        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            //Thread.CurrentThread.CurrentCulture = CurrentEditionCulture;//Sinon en-US
            FilterButton.Click += OnFilterButton;
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            FilterButton.Click += OnFilterButton;
        }

        void OnFilterButton(object sender, EventArgs e)
        {
            OnFilter();
        }

        public override IEnumerable<ListItem> Status
        {
            get { return ContentStatesList.Items.Cast<ListItem>(); }
            set
            {
                ContentStatesList.Items.AddRange(value.ToArray());
            }
        }

        public override string Keywords
        {
            get
            {
                return Name.Text;
            }
            set
            {
                Name.Text = value;
            }
        }

        public IEnumerable<ListItem> Brands
        {
            get { return BrandsList.Items.Cast<ListItem>(); }
            set
            {
                BrandsList.Items.AddRange(value.ToArray());
            }
        }

        public IEnumerable<ListItem> Categories
        {
            get { return CategoryList.Items.Cast<ListItem>(); }
            set
            {
                CategoryList.Items.AddRange(value.ToArray());
            }
        }

        public override DateTime? SelectedStartDateTime
        {
            get { return null; }
            set { }
        }

        public override DateTime? SelectedEndDateTime
        {
            get { return null; }
            set { }
        }

        public override Guid BaseTypeId
        {
            get
            {
                return BaseProduct.TYPE_ID;
            }
        }

        public override Guid ModuleNodeId
        {
            get
            {
                return Modules.Shop;
            }
        }

        public override Guid? ParentNodeId
        {
            get
            {
                return Request.QueryString.GetValue<Guid?>(DefaultParamNames.ParentNodeID) ?? 
                    PageIds.ProductList;
            }
        }

        public override bool IsMultiLingual
        {
            get
            {
                return true;
            }
        }

        public override string ListingPageUrl
        {
            get { return Pages.ProductList; }
        }

        public override string EditPageUrl
        {
            get
            {
                return Pages.Edit;
            }
        }

        public override IMedianamikGrid Grid
        {
            get { return (IMedianamikGrid)ProductsGrid; }
        }

        public override string TypeSelectorUrl
        {
            get
            {
                return "/medianamik/modalwindows/typeselectorwindow.aspx";
            }
        }

        public override HyperLink AddButton
        {
            get { return lnkAddProduct; }
        }
    }

    public interface IProducts : IModuleViewWithGrid
    {
        IEnumerable<ListItem> Brands { get; set; }
        IEnumerable<ListItem> Categories { get; set; }
    }
}
